
 /**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */
package com.sys.midware.redis.core.client;

import java.util.ArrayList;
import java.util.List;

import com.sys.midware.logger.Logger;
import com.sys.midware.logger.LoggerFactory;
import com.sys.midware.redis.config.RedisConfig;
import com.sys.midware.redis.config.RedisConfigHolder;
import com.sys.midware.redis.config.RedisConfigUtil;
import com.sys.midware.redis.config.RedisNode;
import com.sys.midware.redis.config.RedisPoolConfig;
import com.sys.midware.redis.core.RedisFactory;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * <b>Description：</b> redis分片连接池 <br/>
 * <b>ClassName：</b> RedisShardedPoolClient <br/>
 * <b>@author：</b> jackyshang <br/>
 * <b>@date：</b> 2016年8月19日 上午10:49:34 <br/>
 * <b>@version: </b>  <br/>
 */
public class RedisShardedPoolClient {
    private static final Logger logger = LoggerFactory.getLogger(RedisFactory.class);
    /**
     * 单节点的jedisPool
     */
    private static ShardedJedisPool pool = null;

    public static final int DEFAULT_TIMEOUT = 2000;

    /**
     * 初始化Redis连接池
     */
    private static void initPool() {
        if (pool != null) {
            return;
        }
        RedisConfigUtil.initConfig();
        RedisConfig redisConfig = RedisConfigHolder.get();
        RedisPoolConfig poolConfig = redisConfig.getRedisPoolConfig();
        //jedis连接池配置
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(poolConfig.getMaxIdle());
        config.setMaxTotal(poolConfig.getMaxTotal());
        config.setMaxWaitMillis(poolConfig.getMaxWait());
        config.setMinEvictableIdleTimeMillis(poolConfig.getMinEvictableIdleTimeMillis());
        config.setMinIdle(poolConfig.getMinIdle());
        config.setTestOnBorrow(poolConfig.isTestOnBorrow());
        config.setTestOnReturn(poolConfig.isTestOnReturn());
        
        List<JedisShardInfo> jedisShardInfos =new ArrayList<JedisShardInfo>();
        // 初始化jedis,
        for (RedisNode redisNode : redisConfig.getNodeList()) {
            JedisShardInfo shardInfo =new JedisShardInfo(redisNode.getHost(),redisNode.getPort(),DEFAULT_TIMEOUT);
            jedisShardInfos.add(shardInfo);
        }
        pool = new ShardedJedisPool(config,jedisShardInfos);
    }

    /**
     * 同步获取Jedis实例
     * 
     * @return Jedis
     */
    public synchronized static ShardedJedis getJedis() {
        if (pool == null) {
            initPool();
        }
        ShardedJedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Get jedis error : " , e);
        }finally{
            //jedisPool.close();
        }
        return jedis;
    }

    /**
     * 释放jedis资源
     * 
     * @param jedis
     */
    public static void close(final ShardedJedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}
